#include "mainwidget.h"
#include "./ui_mainwidget.h"
#include <QDebug>
#include <QMessageBox>
#include <QHeaderView>
#include <ranges>
#include <utility>
#include "VideoPlatform.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent), ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    Init();

    std::ignore = connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWidget::CurrentTabChanged);
    std::ignore = connect(ui->btnFresh, &QPushButton::clicked, this, &MainWidget::CurrentTabFresh);
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::Init()
{
    dayOfWeekMap.insert(Qt::DayOfWeek::Monday, std::make_tuple("周一", "Mon"));
    dayOfWeekMap.insert(Qt::DayOfWeek::Tuesday, std::make_tuple("周二", "Tue"));
    dayOfWeekMap.insert(Qt::DayOfWeek::Wednesday, std::make_tuple("周三", "Wed"));
    dayOfWeekMap.insert(Qt::DayOfWeek::Thursday, std::make_tuple("周四", "Thu"));
    dayOfWeekMap.insert(Qt::DayOfWeek::Friday, std::make_tuple("周五", "Fri"));
    dayOfWeekMap.insert(Qt::DayOfWeek::Saturday, std::make_tuple("周六", "Sat"));
    dayOfWeekMap.insert(Qt::DayOfWeek::Sunday, std::make_tuple("周日", "Sun"));

    pWidgetTupleList.append(
        std::make_tuple(ui->tab_daily, ui->horizontalLayout_daily, QString("每日更新"), QString("")));
    pWidgetTupleList.append(std::make_tuple(ui->tab_week, ui->horizontalLayout_week, QString("看一周"), QString("")));
    pWidgetTupleList.append(std::make_tuple(ui->tab_tencent, ui->horizontalLayout_tencent, QString("腾讯视频"),
                                            QString("tencent_video")));
    pWidgetTupleList.append(
        std::make_tuple(ui->tab_iQIYI, ui->horizontalLayout_iQIYI, QString("爱奇艺"), QString("iQIYI_video")));
    pWidgetTupleList.append(
        std::make_tuple(ui->tab_youku, ui->horizontalLayout_youku, QString("优酷"), QString("youku_video")));
    pWidgetTupleList.append(std::make_tuple(ui->tab_bilibili, ui->horizontalLayout_bilibili, QString("哔哩哔哩"),
                                            QString("bilibili_video")));
    pWidgetTupleList.append(
        std::make_tuple(ui->tab_maintain, ui->horizontalLayout_maintain, QString("维护"), QString("")));

    auto platformNameViews = pWidgetTupleList | std::views::filter([](const auto &ele) {
        auto [pWidget, pLayout, tabName, platformName] = ele;
        return !platformName.isEmpty();
    }) | std::views::transform([](auto ele) {
        auto [pWidget, pLayout, tabName, platformName] = std::move(ele);
        return platformName;
    });

    platform.InitVideoPlatforms(platformNameViews);
    CurrentTabFresh();
}

void MainWidget::CurrentTabChanged(const int index)
{
    delete pTableWidget;
    const auto &[pTabWidget, pHorizontalLayout, tabName, platformName] = pWidgetTupleList[index];
    pTableWidget = new QTableWidget(pTabWidget);
    pHorizontalLayout->addWidget(pTableWidget);
    pTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    pTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    // pTableWidget->setSelectionMode(QAbstractItemView::NoSelection);
    qDebug() << "Current Tab index：" << index;

    if (platformName.isEmpty()) {
    } else {
        const int count = VideoPlatform::GetSqlTotalCount(platformName);
        // 一周7天加上条数
        pTableWidget->setRowCount(count + 7); // 设置行数
        pTableWidget->setColumnCount(5); // 设置列数

        int row = PlatformOneDay(0, Qt::DayOfWeek::Monday, platformName);
        row = PlatformOneDay(row, Qt::DayOfWeek::Tuesday, platformName);
        row = PlatformOneDay(row, Qt::DayOfWeek::Wednesday, platformName);
        row = PlatformOneDay(row, Qt::DayOfWeek::Thursday, platformName);
        row = PlatformOneDay(row, Qt::DayOfWeek::Friday, platformName);
        row = PlatformOneDay(row, Qt::DayOfWeek::Saturday, platformName);
        PlatformOneDay(row, Qt::DayOfWeek::Sunday, platformName);
    }
}

void MainWidget::CurrentTabFresh()
{
    CurrentTabChanged(ui->tabWidget->currentIndex());
}

int MainWidget::PlatformOneDay(int begin, const Qt::DayOfWeek &dayOfWeek, const QString &tableName)
{
    pTableWidget->setSpan(begin, 0, 1, 5);
    const auto &[dayOfWeekChinese, dayOfWeekEnglish] = dayOfWeekMap[dayOfWeek];
    pTableWidget->setItem(begin, 0, new QTableWidgetItem(dayOfWeekChinese));
    ++begin;

    const int end = begin + VideoPlatform::GetSqlDayCount(tableName, dayOfWeekEnglish);

    if (begin == end) {
        return end;
    }

    auto platformRecordList = VideoPlatform::VideoRecords(tableName, dayOfWeekEnglish);

    for (int i = 0; begin < end; ++i, ++begin) {
        pTableWidget->setItem(begin, 0, new QTableWidgetItem("更新时间：" + platformRecordList[i]->GetUpdateTime()));
        pTableWidget->setItem(begin, 1, new QTableWidgetItem(platformRecordList[i]->GetPlayName()));
        ///////////////////////////////////////////////////
        readWidget = new QWidget(pTableWidget);
        readHorizontalLayout = new QHBoxLayout(readWidget);
        readHorizontalLayout->setContentsMargins(-1, 0, -1, 0);
        readLabel = new QLabel(readWidget);
        readLabel->setText("已读：" + QString::number(platformRecordList[i]->GetReadEpisode()) + " 集");
        readHorizontalLayout->addWidget(readLabel);

        readPushButton = new QPushButton(readWidget);
        readPushButton->setText("+");
        readPushButton->setMaximumSize(QSize(25, 25));
        readHorizontalLayout->addWidget(readPushButton);
        std::ignore = connect(readPushButton, &QPushButton::clicked, [this,i,tableName,platformRecordList]() {
            VideoPlatform::AddAlreadyRead(tableName, platformRecordList[i]->GetId());
            CurrentTabFresh();
        });

        //        readHorizontalSpacer = new QSpacerItem(265, 20, QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Minimum);
        //        readHorizontalLayout->addItem(readHorizontalSpacer);

        pTableWidget->setCellWidget(begin, 2, readWidget);

        ///////////////////////////////////////////////////////////////////////////////
        pTableWidget->setItem(begin, 3, new QTableWidgetItem(
                                  "更新至：" + QString::number(platformRecordList[i]->GetUpdateEpisode()) + " 集"));
        pTableWidget->setItem(begin, 4, new QTableWidgetItem(
                                  "全：" + QString::number(platformRecordList[i]->GetAllEpisode()) + " 集"));
    }

    return end;
}
